package annotationAndReflection;

import java.lang.reflect.Field;

/**
 * @author mubei
 * @date 2021/11/25
 * @description
 */
public class ReflectAccessProperty {

    public static void main(String[] args) throws Exception {
        Class<?> studentClass = Class.forName("annotationAndReflection.Student");
        Student student = (Student)studentClass.newInstance();
        Object o = studentClass.newInstance();
        System.out.println(o.getClass());

        //3.反射获取属性
        Field name = studentClass.getField("name");
        name.set(o,"ah");
        System.out.println(o);

        //使用反射操作age  私有静态
        Field age = studentClass.getDeclaredField("age");
        age.setAccessible(true);
        age.set(o,199);
        System.out.println(o);
    }
}

class Student{
    private static  int age = 18;
    public String name = "mubei";

    public Student(){

    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                "age='" + age + '\'' +
                '}';
    }
}